package io.wdb.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wdb.exchange.constant.EventEnum;
import com.wdb.exchange.constant.SourceEnum;
import com.wdb.exchange.model.Event;

import io.wdb.common.utils.CommonUtil;
import io.wdb.config.AmqpConfig;
import io.wdb.modules.activity.wxapp.entity.HdCardAttenderEntity;
import io.wdb.modules.activity.wxapp.entity.HdLotteryUserInfoEntity;
import io.wdb.modules.activity.wxapp.entity.HdUserEntity;
import io.wdb.modules.activity.wxapp.service.HdActivityService;
import io.wdb.modules.activity.wxapp.service.HdCardAttenderService;
import io.wdb.modules.activity.wxapp.service.HdCardInitiatorService;
import io.wdb.modules.activity.wxapp.service.HdLotteryActivityService;
import io.wdb.modules.activity.wxapp.service.HdLotteryUserInfoService;
import io.wdb.modules.activity.wxapp.service.HdUserService;

@Component
public class ActivityListenHandler {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	HdUserService hdUserService;
	@Autowired
	HdLotteryUserInfoService hdLotteryUserInfoService;
	@Autowired
	HdLotteryActivityService hdLotteryActivityService;
	@Autowired
	AmqpSendHandler 	amqpSendHandler;
	
	@Autowired
	private HdCardInitiatorService hdCardInitiatorService;
	@Autowired
	private HdCardAttenderService hdCardAttenderService;
	
	public void process(Event event) {
		EventEnum eventEnum = event.event;
		logger.info("ApiListenHandler process {}", event);
		event.eventTime = new Date().getTime(); 
		
		// 不处理SourceEnum.ACTIVITY信息
		if (!SourceEnum.ACTIVITY.equals(event.source) && !CommonUtil.isEmpty(event.openId)) {
			HdUserEntity hdUser = new HdUserEntity();
			hdUser.setApiUserId(event.userId);
			hdUser.setMobile(event.mobile);
			hdUser.setWxAppid(event.appId);
			hdUser.setWxOpenid(event.openId);
			String hdUserid = hdUserService.save(hdUser);
			hdUser.setId(hdUserid);
			Map<String, Object> lotteryInfoQuery = new HashMap<String, Object>();
			lotteryInfoQuery.put("filterSql"," hd_user_id = '"+ hdUserid +"' AND lottery_status = 0 AND user_status = 0 AND api_user_id IS NULL");
			
			if(eventEnum.equals(EventEnum.LOGIN)) {//登录
				logger.info("LOGIN,lotteryInfoQuery:{}",lotteryInfoQuery);
				List<HdLotteryUserInfoEntity> hdLotteryUserInfos = hdLotteryUserInfoService.queryList(lotteryInfoQuery);
				if (!CommonUtil.isEmpty(hdLotteryUserInfos)) {
					for (HdLotteryUserInfoEntity lotteryUserinfo : hdLotteryUserInfos) {
						if(CommonUtil.isEmpty(lotteryUserinfo.getApiUserId())) {
							lotteryUserinfo.setApiUserId(hdUser.getApiUserId());
						}
						hdLotteryUserInfoService.sendLotteryAward(lotteryUserinfo,
								hdLotteryActivityService.queryObject(lotteryUserinfo.getHdLotteryActivityId()));
					}
				}
			}else if(eventEnum.equals(EventEnum.REGIST)) {//注册
				logger.info("REGIST,lotteryInfoQuery:{}",lotteryInfoQuery);
				List<HdLotteryUserInfoEntity> hdLotteryUserInfos_REGIST = hdLotteryUserInfoService.queryList(lotteryInfoQuery);
				Event activityEvent = event;
				activityEvent.source = SourceEnum.ACTIVITY;
				activityEvent.event = EventEnum.AWARD_REGISTER;
				// 更新抽奖记录
				if (!CommonUtil.isEmpty(hdLotteryUserInfos_REGIST) && hdLotteryUserInfos_REGIST.size() > 0) {
					for (HdLotteryUserInfoEntity lotteryUserinfo : hdLotteryUserInfos_REGIST) {
						if(CommonUtil.isEmpty(lotteryUserinfo.getApiUserId())) {
							lotteryUserinfo.setApiUserId(hdUser.getApiUserId());
						}
						hdLotteryUserInfoService.sendLotteryAward(lotteryUserinfo,
								hdLotteryActivityService.queryObject(lotteryUserinfo.getHdLotteryActivityId()));
						// 推送消息注册登录统计消息
						amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, activityEvent.toJson());
					}
				}
				
				//发送月卡活动注册数据统计
				try {
					if(!CommonUtil.isEmpty(hdUserid)) {
						//判断是否参与活动
						Map<String, Object> map = new HashMap<>();
						map.put("userId", hdUserid);
						List<HdCardAttenderEntity> hdCardAttenders = hdCardAttenderService.queryList(map);
						if(!CommonUtil.isEmpty(hdCardAttenders) && hdCardAttenders.size() > 0) {
							//===========异步数据统计FISSION_REGISTER START===========//
							try {
								Event eventDate = new Event();
								eventDate.userId = hdUser.getApiUserId();
								eventDate.source = SourceEnum.ACTIVITY;
								eventDate.event = EventEnum.FISSION_REGISTER;
								eventDate.openId = hdUser.getWxOpenid();
								eventDate.appId = hdUser.getWxAppid();
								eventDate.mobile = hdUser.getMobile();
								eventDate.eventTime = new Date().getTime();
								//数据统计
								amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, eventDate.toJson());
							} catch (Exception e) {
								logger.info("FISSION_REGISTER异步数据统计error:{}",e.getMessage());
								e.printStackTrace();
							}
							//===========异步数据统计FISSION_REGISTER END===========//
						}
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else if(eventEnum.equals(EventEnum.DEPOSIT)) {//交押金
				Event activityEvent_DEPOSIT = event;
				activityEvent_DEPOSIT.source = SourceEnum.ACTIVITY;
				activityEvent_DEPOSIT.event = EventEnum.AWARD_DEPOSIT;
				
				Map<String, Object> lotteryInfoQuery_DEPOSIT = new HashMap<>();
				lotteryInfoQuery_DEPOSIT.put("filterSql", " hd_user_id = '"+ hdUserid +"' AND lottery_status = 1 AND api_user_id = "+ event.userId);
				logger.info("DEPOSIT,lotteryInfoQuery_DEPOSIT:{}",lotteryInfoQuery_DEPOSIT);
				List<HdLotteryUserInfoEntity> hdLotteryUserInfos_DEPOSIT = hdLotteryUserInfoService.queryList(lotteryInfoQuery_DEPOSIT);
				if (!CommonUtil.isEmpty(hdLotteryUserInfos_DEPOSIT) && hdLotteryUserInfos_DEPOSIT.size() > 0) {
					amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, activityEvent_DEPOSIT.toJson());
				}
				logger.info("缴纳押金");
				
				//发送月卡活动缴纳押金数据统计
				try {
					if(!CommonUtil.isEmpty(hdUserid)) {
						//判断是否参与活动
						Map<String, Object> map = new HashMap<>();
						map.put("userId", hdUserid);
						List<HdCardAttenderEntity> hdCardAttenders = hdCardAttenderService.queryList(map);
						if(!CommonUtil.isEmpty(hdCardAttenders) && hdCardAttenders.size() > 0) {
							//===========异步数据统计FISSION_DEPOSIT START===========//
							try {
								Event eventDate = new Event();
								eventDate.userId = hdUser.getApiUserId();
								eventDate.source = SourceEnum.ACTIVITY;
								eventDate.event = EventEnum.FISSION_DEPOSIT;
								eventDate.openId = hdUser.getWxOpenid();
								eventDate.appId = hdUser.getWxAppid();
								eventDate.mobile = hdUser.getMobile();
								eventDate.eventTime = new Date().getTime();
								//数据统计
								amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, eventDate.toJson());
							} catch (Exception e) {
								logger.info("FISSION_DEPOSIT异步数据统计error:{}",e.getMessage());
								e.printStackTrace();
							}
							//===========异步数据统计FISSION_DEPOSIT END===========//
						}
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}else if(eventEnum.equals(EventEnum.CHARGE)) {//余额充值
				Event activityEvent_CHARGE = event;
				activityEvent_CHARGE.source = SourceEnum.ACTIVITY;
				activityEvent_CHARGE.event = EventEnum.AWARD_CHARGE;
				
				Map<String, Object> lotteryInfoQuery_CHARGE = new HashMap<>();
				lotteryInfoQuery_CHARGE.put("filterSql", " hd_user_id = '"+ hdUserid +"' AND lottery_status = 1 AND api_user_id = "+ event.userId);
				logger.info("CHARGE,lotteryInfoQuery_DEPOSIT:{}",lotteryInfoQuery_CHARGE);

				List<HdLotteryUserInfoEntity> hdLotteryUserInfos_CHARGE = hdLotteryUserInfoService.queryList(lotteryInfoQuery_CHARGE);
				if (!CommonUtil.isEmpty(hdLotteryUserInfos_CHARGE) && hdLotteryUserInfos_CHARGE.size() > 0) {
						amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, activityEvent_CHARGE.toJson());
				}
				logger.info("充值");
				
				//发送月卡活动充值数据统计
				try {
					if(!CommonUtil.isEmpty(hdUserid)) {
						//判断是否参与活动
						Map<String, Object> map = new HashMap<>();
						map.put("userId", hdUserid);
						List<HdCardAttenderEntity> hdCardAttenders = hdCardAttenderService.queryList(map);
						if(!CommonUtil.isEmpty(hdCardAttenders) && hdCardAttenders.size() > 0) {
							//===========异步数据统计FISSION_CHARGE START===========//
							try {
								Event eventDate = new Event();
								eventDate.userId = hdUser.getApiUserId();
								eventDate.source = SourceEnum.ACTIVITY;
								eventDate.event = EventEnum.FISSION_CHARGE;
								eventDate.openId = hdUser.getWxOpenid();
								eventDate.appId = hdUser.getWxAppid();
								eventDate.mobile = hdUser.getMobile();
								eventDate.eventTime = new Date().getTime();
								//数据统计
								amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, eventDate.toJson());
							} catch (Exception e) {
								logger.info("FISSION_CHARGE异步数据统计error:{}",e.getMessage());
								e.printStackTrace();
							}
							//===========异步数据统计FISSION_CHARGE END===========//
						}
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
			}else if(eventEnum.equals(EventEnum.CANCEL_DEPOSIT)) {//退押金
				Event activityEvent_CANCEL_DEPOSIT = event;
				activityEvent_CANCEL_DEPOSIT.source = SourceEnum.ACTIVITY;
				activityEvent_CANCEL_DEPOSIT.event = EventEnum.AWARD_CANCEL_DEPOSIT;
				
				Map<String, Object> lotteryInfoQuery_CANCEL_DEPOSIT = new HashMap<>();
				lotteryInfoQuery_CANCEL_DEPOSIT.put("filterSql", " hd_user_id = '"+ hdUserid +"' AND lottery_status = 1 AND api_user_id = "+ event.userId);
				logger.info("CANCEL_DEPOSIT,lotteryInfoQuery_DEPOSIT:{}",lotteryInfoQuery_CANCEL_DEPOSIT);

				List<HdLotteryUserInfoEntity> hdLotteryUserInfos_CANCEL_DEPOSIT = hdLotteryUserInfoService.queryList(lotteryInfoQuery_CANCEL_DEPOSIT);
				if (!CommonUtil.isEmpty(hdLotteryUserInfos_CANCEL_DEPOSIT) && hdLotteryUserInfos_CANCEL_DEPOSIT.size() > 0) {
						amqpSendHandler.sendFanout(AmqpConfig.SERVICE_EXCHANGE, activityEvent_CANCEL_DEPOSIT.toJson());
				}
				logger.info("退押金");
			}
		}

	}
}
